package com.elder.business.service.impl;

import java.security.PublicKey;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.*;
import com.elder.business.mapper.ApartmentDictMapper;
import com.elder.business.mapper.ElderlyapartmentMapper;
import com.elder.business.mapper.MemberInfoMapper;
import com.elder.common.core.domain.AjaxResult;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.enums.ApartmentType;
import com.elder.common.enums.ApplyStatus;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.IntegerUtils;
import com.elder.common.utils.LongUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.bean.BeanUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elder.business.mapper.ElderlyApartmentActivitiesMapper;
import com.elder.business.service.IElderlyApartmentActivitiesService;

/**
 * 公寓活动Service业务层处理
 * 
 * @author zyk
 * @date 2023-11-26
 */
@Service
public class ElderlyApartmentActivitiesServiceImpl implements IElderlyApartmentActivitiesService 
{
    @Autowired
    private ElderlyApartmentActivitiesMapper elderlyApartmentActivitiesMapper;
    @Autowired
    private ElderlyapartmentMapper elderlyapartmentMapper;
    @Autowired
    private ApartmentDictMapper apartmentDictMapper;
    @Autowired
    private MemberInfoMapper memberInfoMapper;

    /**
     * 查询公寓活动
     * 
     * @param id 公寓活动主键
     * @return 公寓活动
     */
    @Override
    public JSONObject selectElderlyApartmentActivitiesById(Long id)
    {
        ElderlyApartmentActivities activities = elderlyApartmentActivitiesMapper.selectElderlyApartmentActivitiesById(id);
        Elderlyapartment apartmentInfo = elderlyapartmentMapper.getApartmentInfo(activities.getApartmentId());
        JSONObject obj = new JSONObject();
        obj.put("activityId", StringNewUtils.long2Str(activities.getActivityId()));
        obj.put("apartmentId", StringNewUtils.long2Str(activities.getApartmentId()));
        obj.put("apartmentName", StringNewUtils.formatStr(apartmentInfo.getApartmentName()));
        obj.put("activityDate", DateUtils.dateStr(activities.getActivityDate()));
        obj.put("activityName", activities.getActivityName());
        obj.put("activityDescription", activities.getActivityDescription());
        obj.put("applyStatus", activities.getApplyStatus());
        obj.put("leaderId", StringNewUtils.long2Str(activities.getLeaderId()));
        obj.put("leaderName", activities.getLeaderName());
        obj.put("participantCount", activities.getParticipantCount());
        obj.put("notes", activities.getNotes());
        obj.put("activityUrl",activities.getActivityUrl());
        obj.put("createUserId", StringNewUtils.long2Str(activities.getCreateUserId()));
        obj.put("updateUserId", StringNewUtils.long2Str(activities.getUpdateUserId()));
        obj.put("activityPrice", StringNewUtils.isEmpty(activities.getActivityPrice())?"0":activities.getActivityPrice());
        return obj;
    }

    /**
     * 查询公寓活动列表
     * 
     * @param elderlyApartmentActivities 公寓活动
     * @return 公寓活动
     */
    @Override
    public List<ElderlyApartmentActivities> selectElderlyApartmentActivitiesList(ElderlyApartmentActivities elderlyApartmentActivities)
    {
        return elderlyApartmentActivitiesMapper.selectElderlyApartmentActivitiesList(elderlyApartmentActivities);
    }

    /**
     * 新增公寓活动
     * 
     * @param activityVO 公寓活动
     * @return 结果
     */
    @Override
    public int insertElderlyApartmentActivities(ElderlyApartmentActivitiesVO activityVO)
    {
        ElderlyApartmentActivities activity = new ElderlyApartmentActivities();
        BeanUtils.copyProperties(activityVO, activity);
        activity.setActivityId(SnowGenerator.getId());
        activity.setApartmentId(LongUtils.str2Long(activityVO.getApartmentId()));
        activity.setActivityDate(DateUtils.getTargetDateSeconds(activityVO.getActivityDate()));
        activity.setApplyStatus(ApplyStatus.APPLUING.getValue());
        activity.setLeaderId(LongUtils.str2Long(activityVO.getLeaderId()));
        activity.setParticipantCount(LongUtils.str2Long(activityVO.getParticipantCount()));
        activity.setCreateUserId(LongUtils.str2Long(activityVO.getCreateUserId()));
        activity.setCreateTime(DateUtils.getLongTime());
        return elderlyApartmentActivitiesMapper.insertElderlyApartmentActivities(activity);
    }

    /**
     * 修改公寓活动
     * 
     * @param activityVO 公寓活动
     * @return 结果
     */
    @Override
    public int updateElderlyApartmentActivities(ElderlyApartmentActivitiesVO activityVO)
    {
        ElderlyApartmentActivities activity = new ElderlyApartmentActivities();
        BeanUtils.copyProperties(activityVO, activity);
        activity.setActivityId(LongUtils.str2Long(activityVO.getActivityId()));
        activity.setApartmentId(LongUtils.str2Long(activityVO.getApartmentId()));
        activity.setActivityDate(DateUtils.getTargetDateSeconds(activityVO.getActivityDate()));
//        activity.setApplyStatus(IntegerUtils.toInteger(activityVO.getApplyStatus()));
        activity.setLeaderId(LongUtils.str2Long(activityVO.getLeaderId()));
        activity.setParticipantCount(LongUtils.str2Long(activityVO.getParticipantCount()));
        activity.setUpdateTime(DateUtils.getLongTime());
        return elderlyApartmentActivitiesMapper.updateElderlyApartmentActivities(activity);
    }

    /**
     * 批量删除公寓活动
     * 
     * @param ids 需要删除的公寓活动主键
     * @return 结果
     */
    @Override
    public int deleteElderlyApartmentActivitiesByIds(Long[] ids)
    {
        return elderlyApartmentActivitiesMapper.deleteElderlyApartmentActivitiesByIds(ids);
    }

    /**
     * 删除公寓活动信息
     * 
     * @param id 公寓活动主键
     * @return 结果
     */
    @Override
    public int deleteElderlyApartmentActivitiesById(Long id)
    {
        return elderlyApartmentActivitiesMapper.deleteElderlyApartmentActivitiesById(id);
    }

    @Override
    public TableDataInfo getApartmentActivitiesList(String startTime,String endTime, String activityName, String leaderName, Integer pageNum, Integer pageSize,String applyStatus) {
        //日期处理
        Long start = null;
        Long end = null;
        if(!StringNewUtils.isEmpty(startTime) && !StringNewUtils.isEmpty(endTime)){
            start = DateUtils.getDayStartTime(DateUtils.getTargetDateSeconds(startTime));
            end = DateUtils.getDayEndTime(DateUtils.getTargetDateSeconds(endTime));
        }
        Integer status = ApplyStatus.APPLY_PASS.getValue();
        if(!StringNewUtils.isEmpty(applyStatus)){
            status = ApplyStatus.APPLUING.getValue();
        }
        //分页数据
        PageHelper.startPage(pageNum,pageSize);
        List<ElderlyApartmentActivities> list = elderlyApartmentActivitiesMapper.getApartmentActivitiesList(start, end, activityName, leaderName,status);
        PageInfo<ElderlyApartmentActivities> pageInfo = new PageInfo<>(list);
        List<ElderlyApartmentActivities> activitiesList = pageInfo.getList();

//        List<Long> leaderIds = activitiesList.stream().filter(rel -> Objects.nonNull(rel.getLeaderId()))
//                .map(ElderlyApartmentActivities::getLeaderId).distinct().collect(Collectors.toList());
        //获取公寓负责人
//        List<MemberInfo> memberUserList = memberInfoMapper.getMemberListByMemberIds(leaderIds);
//        Map<Long, MemberInfo> memberMap = memberUserList.stream().collect(Collectors.toMap(MemberInfo::getMemberId, Function.identity()));
        //获取公寓信息
        Map<Long, String> apartMap = elderlyapartmentMapper.getAllApartmentInfo().stream()
                .collect(Collectors.toMap(Elderlyapartment::getApartmentId, Elderlyapartment::getApartmentName));

        //获取公寓字典数据
//        Map<Integer, String> dictMap = apartmentDictMapper.getApartmentDictInfo(ApartmentType.ACTIVITIES_APPLY_STATUS.getValue()).stream()
//                .collect(Collectors.toMap(ApartmentDict::getCode, ApartmentDict::getName));

        List<JSONObject> actList = activitiesList.stream().map(v -> {
            JSONObject obj = new JSONObject();
            obj.put("activityId", StringNewUtils.long2Str(v.getActivityId()));
            obj.put("apartmentId", StringNewUtils.long2Str(v.getApartmentId()));
            obj.put("apartmentName", apartMap.get(v.getApartmentId()));
            obj.put("activityDate", DateUtils.dateStr(v.getActivityDate()));
            obj.put("activityName", v.getActivityName());
            obj.put("activityDescription", v.getActivityDescription());
            obj.put("applyStatus", ApplyStatus.getCnName(v.getApplyStatus()));
            obj.put("activityPrice", v.getActivityPrice());
            obj.put("leaderId", StringNewUtils.long2Str(v.getLeaderId()));
            obj.put("leaderName", v.getLeaderName());
            obj.put("participantCount", Objects.isNull(v.getParticipantCount())?"0": v.getParticipantCount());
            obj.put("notes", v.getNotes());
            obj.put("activityUrl", StringNewUtils.formatStr(v.getActivityUrl()));
            obj.put("createUserId", v.getCreateUserId());
            obj.put("updateUserId", v.getUpdateUserId());
            return obj;
        }).collect(Collectors.toList());
        TableDataInfo result = new TableDataInfo();
        result.setCode(200);
        result.setMsg("success");
        result.setTotal(pageInfo.getTotal());
        result.setRows(actList);
        return result;
    }

    @Override
    public JSONObject uploadActVideo(ElderlyApartmentActivitiesVO elderActivities) {
        //上传视频
        elderlyApartmentActivitiesMapper.updateVideoActivitiesUrl(LongUtils.str2Long(elderActivities.getActivityId()), elderActivities.getActivityUrl());
        JSONObject obj = new JSONObject();
        obj.put("code",200);
        return obj;
    }

    @Override
    public JSONObject getActivitiesVideo(Long activityId) {
        ElderlyApartmentActivitiesVO elderVo = new ElderlyApartmentActivitiesVO();
        ElderlyApartmentActivities apartmentActivities = elderlyApartmentActivitiesMapper.getApartmentActivitiesByActivityId(activityId);
        BeanUtils.copyProperties(apartmentActivities,elderVo);
        elderVo.setActivityId(StringNewUtils.long2Str(apartmentActivities.getActivityId()));
        elderVo.setApartmentId(StringNewUtils.long2Str(apartmentActivities.getApartmentId()));
        elderVo.setApplyStatus(StringNewUtils.int2Str(apartmentActivities.getApplyStatus()));
        JSONObject obj = new JSONObject();
        obj.put("code",200);
        obj.put("msg","success");
        obj.put("activityUrl",apartmentActivities.getActivityUrl());
        obj.put("data",elderVo);
        return obj;
    }

    public AjaxResult approval(Long activityId, String applyStatus, String notes)    {
        elderlyApartmentActivitiesMapper.updateActivitiesApply(activityId, ApplyStatus.getValue(applyStatus), notes);
        return AjaxResult.success();
    }
}
